using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class OrderController : ControllerBase
    {
        private readonly IOrderService _orderService;

        public OrderController(IOrderService orderService)
        {
            _orderService = orderService;
        }

        [HttpGet]
        public async Task<ActionResult<List<Order>>> GetAllOrders()
        {
            var orders = await _orderService.GetAllAsync();
            return Ok(orders);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<Order>> GetOrderById(Guid id)
        {
            var order = await _orderService.GetByIdAsync(id);
            if (order == null)
                return NotFound();

            return Ok(order);
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<List<Order>>> GetOrdersByUserId(Guid userId)
        {
            var orders = await _orderService.GetOrdersByUserIdAsync(userId);
            return Ok(orders);
        }

        [HttpGet("status/{status}")]
        public async Task<ActionResult<List<Order>>> GetOrdersByStatus(byte status)
        {
            var orders = await _orderService.GetOrdersByStatusAsync(status);
            return Ok(orders);
        }

        [HttpGet("{orderId}/total")]
        public async Task<ActionResult<decimal>> GetOrderTotal(Guid orderId)
        {
            try
            {
                var total = await _orderService.CalculateOrderTotalAsync(orderId);
                return Ok(total);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpPost]
        public async Task<ActionResult<Order>> CreateOrder(Order order)
        {
            var createdOrder = await _orderService.CreateAsync(order);
            return CreatedAtAction(nameof(GetOrderById), new { id = createdOrder.OrderId }, createdOrder);
        }

        [HttpPut("{id}")]
        public async Task<ActionResult<Order>> UpdateOrder(Guid id, Order order)
        {
            if (id != order.OrderId)
                return BadRequest();

            try
            {
                var updatedOrder = await _orderService.UpdateAsync(order);
                return Ok(updatedOrder);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpPut("{id}/status/{status}")]
        public async Task<ActionResult<Order>> UpdateOrderStatus(Guid id, byte status)
        {
            try
            {
                var updatedOrder = await _orderService.UpdateOrderStatusAsync(id, status);
                return Ok(updatedOrder);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpPost("{orderId}/items")]
        public async Task<ActionResult<Order>> AddOrderItem(Guid orderId, OrderItem item)
        {
            try
            {
                var updatedOrder = await _orderService.AddOrderItemAsync(orderId, item);
                return Ok(updatedOrder);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpDelete("{orderId}/items/{itemId}")]
        public async Task<ActionResult<Order>> RemoveOrderItem(Guid orderId, Guid itemId)
        {
            try
            {
                var updatedOrder = await _orderService.RemoveOrderItemAsync(orderId, itemId);
                return Ok(updatedOrder);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteOrder(Guid id)
        {
            var result = await _orderService.DeleteAsync(id);
            if (!result)
                return NotFound();

            return NoContent();
        }
    }
} 